<template>
  <div class="pwdPay">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" status-icon label-width="70px" class="pwd-form">
      <el-form-item v-show="isSet" label="原密码" prop="oldpass">
        <el-input v-model="ruleForm.oldpass" type="password" show-password />
      </el-form-item>
      <el-form-item label="新密码" prop="newpass">
        <el-input v-model="ruleForm.newpass" type="password" show-password />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="ruleForm.checkPass" type="password" show-password />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native="submitForm">{{ isSet ? '修改' : '设置' }}</el-button>
        <el-button @click.native="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { encryptByAES } from '@/utils/aes'
import { setPayPwd } from '@/api/table'
import { setToken, getToken } from '@/utils/auth'
import { isvalidPayPwd } from '@/utils/validate'
export default {
  data() {
    // var validatePass = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请输入密码'))
    //   } else if (!isvalidPayPwd(value)) {
    //     callback(new Error('支付密码由6位数字组成'))
    //   } else {
    //     callback()
    //   }
    // }
    var validatePass1 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (!isvalidPayPwd(value)) {
        callback(new Error('支付密码由6位数字组成'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.newpass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      isSet: false,
      ruleForm: {
        oldpass: '',
        newpass: '',
        checkPass: ''
      },
      rules: {
        // oldpass: [
        //   { validator: validatePass, trigger: 'blur' }
        // ],
        newpass: [
          { validator: validatePass1, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    if (getToken('isSet') === '1') {
      // 已设置支付密码
      this.isSet = true
    } else {
      // 未设置支付密码
      this.isSet = false
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.isSet) {
            setPayPwd({
              pay_password: encryptByAES(this.ruleForm.oldpass),
              newPassword: encryptByAES(this.ruleForm.newpass)
            }).then(response => {
              this.$message({
                message: '修改支付密码成功',
                type: 'success'
              })
              setToken('isSet', '1')
              this.resetForm()
            })
          } else {
            setPayPwd({
              pay_password: encryptByAES(this.ruleForm.newpass)
            }).then(response => {
              this.$message({
                message: '设置支付密码成功',
                type: 'success'
              })
              setToken('isSet', '1')
              this.resetForm()
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.pwdPay{
  padding: 30px;
  width: 500px;
  max-width: 100%;
}
</style>
